package com.server.start;


import com.register.client.domian.RegisterRequest;
import com.register.client.domian.Response;
import com.server.entity.Registry;
import com.server.entity.ServiceInstance;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping("/register")
public class CaptainServerRegister {
    public static final Logger log = LoggerFactory.getLogger(CaptainServerRegister.class);

    /**
     * 服务注册
     * @param registerRequest
     * @return
     */
    @RequestMapping("/initRegister")
    public Response register(@RequestBody RegisterRequest registerRequest) {

        try {
            ServiceInstance serviceInstance = converToServiceInstance(registerRequest);
            Registry.getInstance().registry(serviceInstance);
        } catch (Exception e) {
            log.warn("服务注册失败[{}]", e);
            return Response.FAILE;
        }

        return Response.SUCCESS;
    }

    private ServiceInstance converToServiceInstance(RegisterRequest registerRequest) {

        ServiceInstance serviceInstance = new ServiceInstance();
        BeanUtils.copyProperties(registerRequest, serviceInstance);
        return serviceInstance;
    }
}
